<template>
  <view class="container">
    <!-- 搜索栏 -->
    <SearchBar @search="handleSearch" />

    <!-- 加载状态 -->
    <view v-if="isLoading" class="loading">
      <text>加载中...</text>
    </view>

    <!-- 空数据提示 -->
    <view v-else-if="filteredConsultants.length === 0" class="empty">
      <text>暂无咨询师信息</text>
    </view>

    <!-- 咨询师信息列表 -->
    <view v-else class="consultant-list">
      <view v-for="(consultant, index) in filteredConsultants" :key="index" class="consultant-card">
        <!-- 咨询师头像 -->
        <image class="avatar" :src="consultant.psy_pic" mode="aspectFill"></image>
        
        <!-- 咨询师详细信息 -->
        <view class="consultant-info">
          <!-- 姓名和ID -->
          <view class="name-id">
            <text class="name">{{ consultant.psy_name }}</text>
            <text class="id">(ID: {{ consultant.psy_id }})</text>
          </view>
          
          <!-- 按钮 -->
          <view class="buttons">
            <button class="btn-details" @click="viewDetails(consultant)">详情</button>
            <button class="btn-book" @click="bookOnline(consultant)">线上预约</button>
            <button class="btn-book" @click="bookOffline(consultant)">线下预约</button>
          </view>
          <!-- 引入TabBar组件 -->
          <TabBar :tabs="tabList" :initialSelectedIndex="1" />
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import SearchBar from '@/pages/components/SearchBar.vue';
import TabBar from '@/pages/components/TabBar.vue';

export default {
  components: {
    SearchBar,
    TabBar
  },
  data() {
    return {
      consultants: [], // 从数据库获取的数据
      filteredConsultants: [], // 根据筛选条件过滤后的咨询师列表
      isLoading: false, // 加载状态
      searchKeyword: '', // 搜索关键词
      tabList: [
        {
          pagePath: '/pages/psy/index',
          text: '首页',
          icontype: 'home',
          selectedIcontype: 'home-filled'
        },
        {
          pagePath: '',
          text: '咨询',
          icontype: 'calendar',
          selectedIcontype: 'calendar-filled'
        },
        {
          pagePath: '/pages/psy/my/my',
          text: '我的',
          icontype: 'person',
          selectedIcontype: 'person-filled'
        }
      ]
    };
  },
  created() {
    this.fetchConsultants();
  },
  methods: {
    // 获取咨询师列表
    async fetchConsultants() {
      this.isLoading = true;
      try {
        const res = await uni.request({
          url: `http://127.0.0.1:5001/api_1_0/psyConsultant`,
          method: 'GET'
        });
        if (res.data.code === '2000') {
          this.consultants = res.data.data;
          this.filteredConsultants = this.consultants; // 初始化过滤后的列表
        } else {
          uni.showToast({ title: '获取咨询师列表失败', icon: 'none' });
        }
      } catch (err) {
        uni.showToast({ title: '网络错误，请稍后重试', icon: 'none' });
      } finally {
        this.isLoading = false;
      }
    },
    // 根据关键词过滤咨询师列表
    filterConsultants() {
      if (this.searchKeyword) {
        this.filteredConsultants = this.consultants.filter(
          (consultant) =>
            consultant.psy_name.includes(this.searchKeyword) || // 匹配姓名
            consultant.psy_id.toString().includes(this.searchKeyword) // 匹配ID
        );
      } else {
        this.filteredConsultants = this.consultants;
      }
    },
    // 处理搜索
    handleSearch(keyword) {
      this.searchKeyword = keyword;
      this.filterConsultants();
    },
    // 查看详情
    viewDetails(consultant) {
      uni.navigateTo({
        url: `/pages/psy/consult/introduction?id=${consultant.psy_id}`
      });
    },
    // 线上预约
    bookOnline(consultant) {
      uni.navigateTo({
        url: `/pages/psy/consult/order_online?id=${consultant.psy_id}`
      });
    },
    // 线下预约
    bookOffline(consultant) {
      uni.navigateTo({
        url: `/pages/psy/consult/order_offline?id=${consultant.psy_id}`
      });
    }
  }
};
</script>

<style scoped>
.container {
  padding: 20px;
  padding-bottom: 50px; /* 为了不被TabBar遮挡，留出一些空间 */
}

.loading,
.empty {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
  font-size: 16px;
  color: #666;
}

/* 咨询师卡片样式 */
.consultant-card {
  display: flex;
  align-items: center;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  padding: 15px;
  margin-bottom: 15px;
  margin-top:10px;
}

/* 咨询师头像样式 */
.avatar {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  margin-right: 15px;
}

/* 咨询师信息容器样式 */
.consultant-info {
  flex: 1;
  display: flex;
  flex-direction: column;
}

/* 姓名和ID样式 */
.name-id {
  display: flex;
  align-items: baseline;
  margin-bottom: 8px;
}

.name {
  font-size: 18px;
  font-weight: bold;
  margin-right: 8px;
}

.id {
  font-size: 14px;
  color: #666;
}

/* 按钮容器样式 */
.buttons {
  display: flex;
  gap: 10px;
}

/* 按钮样式 */
.btn-details,
.btn-book {
  flex: 1;
  padding: 8px 12px;
  border-radius: 4px;
  font-size: 14px;
  text-align: center;
  cursor: pointer;
}

.btn-details {
  background-color: #f0f0f0;
  color: #333;
}

.btn-book {
  background-color: #81b337;
  color: #fff;
}
</style>